package com.yn.xgame.logic.util;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;

import com.mzlion.easyokhttp.HttpClient;
import com.yn.xgame.logic.common.UCPassConst;

/**云之讯短信验证码（短信通知工具）
 * @author eric
 *
 */
public class SendMsgUtil {
	private static String getSig(){
		String sig = getMd5(UCPassConst.ACCOUNTSID + UCPassConst.AUTHTOKEN + getTs()).toUpperCase();// 签名算法
		return sig;
	}
	
	private static String getAuth(){
		String auth =new String( Base64.getEncoder().encode(new String(UCPassConst.ACCOUNTSID + ":" + getTs())
		.getBytes()));
		return auth;
	}
	
	private static String getTs() {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
		String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
		return date;
	}

	/**MD5算法
	 * @param plainText
	 * @return
	 */
	private static String getMd5(String plainText) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();

			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			// 32位加密
			return buf.toString();
			// 16位的加密
			// return buf.toString().substring(8, 24);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}

	}

	/**短信发送api
	 * @param params 短信变量{1}{2}{3}的对应参数，以逗号分隔
	 * @param tempId 短信模板id
	 * @param toWho  手机号
	 * @return
	 * @throws IOException
	 */
	public static String sendMsg(
			String params, String tempId, String toWho) throws IOException {
		String json = "{'templateSMS':{'appId':'"+UCPassConst.SMSAPPID+"','param':'"
				+ params
				+ "','templateId':'"
				+ tempId
				+ "','to':'"
				+ toWho
				+ "'}}";
		return HttpClient.textBody(UCPassConst.SMSURL).header("Accept", "application/json")
				.header("Content-Type", "application/json;charset=utf-8")
				.header("Authorization", getAuth()).queryString("sig", getSig())
				.json(json).execute().asString();
	}
	/**client注册
	 * @param mobile
	 * @return
	 * @throws IOException
	 */
	public static String reg(String mobile) throws IOException {
		String json = "{'client':{'appId':'"+UCPassConst.SMSAPPID+"','charge':'0','mobile':'"+mobile+"','clientType':'1','friendlyName':''"
				+ "}}";
		return HttpClient.textBody(UCPassConst.REGCLIENTURL).header("Accept", "application/json")
				.header("Content-Type", "application/json;charset=utf-8")
				.header("Authorization", getAuth()).queryString("sig", getSig())
				.json(json).execute().asString();
	}
	public static void main(String[] args) throws IOException{
		SendMsgUtil.sendMsg("1234", "39550", "15328894967");
	}
}
